﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Vincent
{
    /// <summary>
    /// 正则有关
    /// </summary>
    public class _Regex
    {
        /// <summary>
        /// 查询出str字符串中，出现过多少次sign子串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="sign"></param>
        /// <returns></returns>
        public static int Regex_GetFindFrequency(string str, string sign)
        {
            int num = 0;      // 出现0次

            MatchCollection mc = Regex.Matches(str, sign.ToString(), RegexOptions.None);

            foreach (Match m in mc)
            {
                num++;
            }

            return num;
        }

        /// <summary>
        /// 取出字符串中的数字
        /// </summary>
        /// <returns></returns> 
        public static int Regex_GetNumToString(string str)
        {
            Match m = Regex.Match(str, @"\d");
            int num = _Convert.ToInt(m.Value, 0);

            return num;
        }

        /// <summary>
        /// str字符串中，是否存在(regex)该正则的内容
        /// </summary>
        public static bool Regex_IsContains(string str, string regex)
        {
            bool flag = false;

            MatchCollection mc = Regex.Matches(str, regex, RegexOptions.None);

            foreach (Match m in mc)
            {
                flag = true;
                break;
            }

            return flag;
        }

        /// <summary>
        /// 解析UBB代码
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string DeUbbCode(string content)
        {
            if (string.IsNullOrEmpty(content))
            {
                return "";
            }
            string str = "lxquery=lxquery";
            content = HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(content));
            content = Regex.Replace(content, @"[\r\t\n\$]", "");
            content = Regex.Replace(content, "(///| {2,})", "<br/>");
            if (content.Contains("(/url)"))
            {
                content = Regex.Replace(content, @"\(url=(?<url>[^\)]*)\)\(img/\)(?<urlname>((?!\(/img\)).)*)\(/img\)\(/url\)", "<a href=\"${url}\"><img src=\"${urlname}\" alt=\".\"/></a>");
                content = Regex.Replace(content, @"\(url=(?<url>[^\)]*)\)(?<urlname>((?!\(/url\)).)*)\(/url\)", "<a href=\"${url}\">${urlname}</a>");
                content = Regex.Replace(content, "href=\"(?<url>(((?!http://))|http://(\\w+\\.)+lexun\\.com)[^\\?]*\\?[^\"]*)\"", "href=\"${url}&amp;" + str + "\"");
                content = Regex.Replace(content, "href=\"(?<url>(((?!http://))|http://(\\w+\\.)+lexun\\.com)[^\\?^\"]*)\"", "href=\"${url}?" + str + "\"");
            }
            content = Regex.Replace(content, @"\(img/\)(?<url>((?!\(/img\)).)*)\(/img\)", "<img src=\"${url}\" alt=\".\"/>");
            content = UBBFont(content);
            if (content.Contains("(/phone)"))
            {
                content = Regex.Replace(content, @"\(phone=(?<phone>[^\)]*)\)(?<name>((?!\(/phone\)).)*)\(/phone\)", "<a href=\"wtai://wp/mc;${phone}\">${name}</a>");
            }
            return content;
        }

        /// <summary>
        /// ubb替换
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string UBBFont(string content)
        {
            if (content.Contains("(b)"))
            {
                content = Regex.Replace(content, @"\(b\)(?<content>((?!\(/b\)).)*)\(/b\)", "<b>${content}</b>");
            }
            if (content.Contains("(i)"))
            {
                content = Regex.Replace(content, @"\(i\)(?<content>((?!\(/i\)).)*)\(/i\)", "<i>${content}</i>");
            }
            if (content.Contains("(u)"))
            {
                content = Regex.Replace(content, @"\(u\)(?<content>((?!\(/u\)).)*)\(/u\)", "<u>${content}</u>");
            }
            if (content.Contains("(small)"))
            {
                content = Regex.Replace(content, @"\(small\)(?<content>((?!\(/small\)).)*)\(/small\)", "<small>${content}</small>");
            }
            if (content.Contains("(big)"))
            {
                content = Regex.Replace(content, @"\(big\)(?<content>((?!\(/big\)).)*)\(/big\)", "<big>${content}</big>");
            }
            return content;
        }

    }
}
